package com.zzw.system.controller;


import com.zzw.common.result.Result;
import com.zzw.model.system.SysMenu;
import com.zzw.vo.system.AssginMenuVo;
import com.zzw.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zzw
 * @since 2023-04-21
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    //    菜单列表（树形）
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    public Result findNodes() {
        List<SysMenu> sysMenuList = sysMenuService.findNodes();

        return Result.ok(sysMenuList);
    }


    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation("菜单添加")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu) {
        if (sysMenuService.save(sysMenu)) {
            return Result.ok();
        }
        return Result.fail();
    }


    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation("根据id查询菜单")
    @GetMapping("findNodeById/{id}")
    public Result findNodeById(@PathVariable Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation("菜单修改")
    @PostMapping("update")
    public Result update(@RequestBody SysMenu sysMenu) {
        if (sysMenuService.updateById(sysMenu)) {
            return Result.ok();
        }
        return Result.fail();
    }
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation("根据id删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
//        判断是否有子菜单才可删除
        if (sysMenuService.removeById(id)) {
            return Result.ok();
        }
        return Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation("批量删除菜单")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        if (sysMenuService.removeByIds(ids)) {
            return Result.ok();
        }
        return Result.fail();
    }

    //    status =》 1：可用； 0：禁用
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation("更改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        if (sysMenuService.updateStatus(id, status)) {
            return Result.ok();
        }
        return Result.fail();
    }


    @ApiOperation("根据角色获取菜单数据")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }


    @ApiOperation(value = "根据角色分配菜单")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }
}
